package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }


    /**
     * 品牌的分页查询，参数，
     *
     * @param page 当前页码
     * @param rows 页容量
     * @param key  当前查询的查询条件
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key) {

        return ResponseEntity.ok(this.brandService.queryBrandByPage(page, rows, key));
    }

    /**
     * 根据品牌id查询对应的品牌对象
     *
     * @param brandId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long brandId) {

        return ResponseEntity.ok(new BrandDTO(this.brandService.getById(brandId)));
    }

    /**
     * 根据品牌的id集合，查询对应的品牌集合，List<Entity>=====》List<DTO>
     *
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }


    /**
     * 根据分类id查询对应的品牌集合
     *
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long cid) {

        return ResponseEntity.ok(this.brandService.queryBrandByCategory(cid));
    }

    /**
     * 品牌新增
     * 四个参数
     *
     * @param name        品牌名称
     * @param letter      品牌首字母
     * @param categoryIds 品牌对应的分类id集合字符串
     * @param image       品牌logo
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(
            @RequestParam("name") String name,
            @RequestParam("letter") String letter,
            @RequestParam("categoryIds") String categoryIds,
            @RequestParam(value = "image", required = false) String image) {


        this.brandService.addBrand(name, letter, categoryIds, image);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 品牌新增
     * 四个参数
     *
     * @param id          品牌id
     * @param name        品牌名称
     * @param letter      品牌首字母
     * @param categoryIds 品牌对应的分类id集合字符串
     * @param image       品牌logo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(
            @RequestParam("id") Long id,
            @RequestParam("name") String name,
            @RequestParam("letter") String letter,
            @RequestParam("categoryIds") String categoryIds,
            @RequestParam(value = "image", required = false) String image) {


        this.brandService.updateBrand(id, name, letter, categoryIds, image);

        return ResponseEntity.ok().build();
    }

    @Delete("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id) {

        //TODO 删除品牌时，①能不能删除,（当前品牌下是否有商品），②，品牌分类的中间关系表要同步删除
        brandService.removeById(id);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
